package com.mls.fivedistrict.controller.backend;


import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.pojo.TaskReward;
import com.mls.fivedistrict.server.TaskRewardService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-05-24
 */
@RestController
@RequestMapping("/api/manager")
public class TaskRewardController {
    @Autowired
    private TaskRewardService taskRewardService;

    @RequestMapping("/v1/taskRewards")
    public ServiceResponse getList() {
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List<TaskReward> taskRewardList = taskRewardService.selectList(pageFilter.getWrapper());
        PageInfo pageInfo = new PageInfo(taskRewardList);
        return ServiceResponse.createSuccessResponse(pageInfo);
    }

    @PostMapping("/v1/taskReward")
    public ServiceResponse insert(@RequestBody TaskReward taskReward) {
        BeanValidator.check(taskReward);
        boolean insert = taskRewardService.insertOrUpdate(taskReward);
        if (!insert) {
            return ServiceResponse.createErrorResponse("插入出错");
        }
        return ServiceResponse.createSuccessResponse("插入成功");
    }

    @GetMapping("/v1/taskReward/{taskRewardId}")
    public ServiceResponse edit(@PathVariable("taskRewardId") Long taskRewardId) {
        TaskReward taskReward = taskRewardService.selectOne(Condition.create().eq("task_id", taskRewardId).eq("is_delete", false));
        return ServiceResponse.createSuccessResponse(taskReward);
    }


    @PutMapping("/v1/taskReward/{taskRewardId}")
    public ServiceResponse edit(@PathVariable("taskRewardId") Long taskRewardId, @RequestBody TaskReward taskReward) {
        taskReward.setId(taskRewardId);
        boolean update = taskRewardService.updateById(taskReward);
        if (!update) {
            return ServiceResponse.createErrorResponse("修改出错");
        }
        return ServiceResponse.createSuccessResponse("修改成功");
    }

    @DeleteMapping("/v1/taskReward/{taskRewardId}")
    public ServiceResponse delete(@PathVariable("taskRewardId") Long taskRewardId) {
        TaskReward taskReward = new TaskReward();
        taskReward.setId(taskRewardId);
        taskReward.setDelete(true);
        boolean update = taskRewardService.updateById(taskReward);
        if (!update) {
            return ServiceResponse.createErrorResponse("修改出错");
        }
        return ServiceResponse.createSuccessResponse("修改成功");
    }
}

